package com.lanlan.pdfwatermark;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class TestF {
    public static void main(String[] args) throws Exception {
	String tarPdfPath = "C:\\Users\\XGJ\\Desktop\\reportCopy.pdf";
	String srcPdfPath = "C:\\Users\\XGJ\\Desktop\\report1.pdf";

	addWatermar(srcPdfPath, tarPdfPath, "QZing奇秦", 2, 3, 0.2f, 20);
    }

    /**
     * 
     * @Title: addWatermar
     * @Description: TODO(描述)
     * @param srcPdfPath       原文件地址
     * @param tarPdfPath       生成的地址
     * @param waterMarkContent 内容
     * @param intensiveSize    对角线以下几条
     * @param intensive        每条多少个
     * @param transparency     透明度
     * @param fontSzie 字体大小
     * @throws Exception
     * @author XGJ
     * @date 2022-12-08 08:20:50
     */
    public static void addWatermar(String srcPdfPath, String tarPdfPath, String waterMarkContent, int intensiveSize,
	    int intensive, float transparency, int fontSzie) throws Exception {
	PdfReader reader = new PdfReader(srcPdfPath);
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tarPdfPath));
	PdfGState gs = new PdfGState();

	// 设置字体
	BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	// 设置透明度
	gs.setFillOpacity(transparency);
	int total = reader.getNumberOfPages() + 1;
	PdfContentByte content;
	for (int i = 1; i < total; i++) {
	    content = stamper.getOverContent(i);
	    content.beginText();
	    content.setGState(gs);
	    // 水印颜色
	    content.setColorFill(BaseColor.DARK_GRAY);
	    // 水印字体样式和大小
	    content.setFontAndSize(font, fontSzie);
	    // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
	    Rectangle pageSize = reader.getPageSize(i);
	    float height = pageSize.getHeight();
	    float width = pageSize.getWidth();
	    System.out.println("--height------" + height);
	    System.out.println("--width-------" + width);
	    //y = kx + b
	    float b = height;
	    float k = -height/width;
	    // 插入水印 循环每页插入的条数
		float x = width;
		float y = height;
		float x1;
		float y1;
		int k1 = (int) (height/(intensiveSize +1));
		for (int j = 0; j <= intensive; j++) {
			y1 = y - (y/intensive*j);
			x1 =  (y - (y/intensive*j) - b)/k;
		    System.out.println("--x-------" + j + ":" + x1);
		    System.out.println("--y-------" + j + ":" + y1);

		    content.showTextAligned(Element.ALIGN_CENTER, waterMarkContent, x1,
		    		y1, 45);
		    for (int z = 1; z <= intensiveSize; z++) {
		    	content.showTextAligned(Element.ALIGN_CENTER, waterMarkContent, x1,
			    		y1-(k1*z), 45);
		    	content.showTextAligned(Element.ALIGN_CENTER, waterMarkContent, x1,
			    		y1+(k1*z), 45);
			}
		    
		}
	    
	    content.endText();
	}
	stamper.close();
	System.out.println("PDF水印添加完成！");
    }
}
